// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: null,
    password:null,
    isselectde:true,
    currentTime:60,
    disabled:false,
    time:'获取验证码'
  },
  isshow(){
    this.setData({
      isselectde:!this.data.isselectde
    })
  },
  // 跳转找回密码
  forgetpass(){
    wx.navigateTo({
      url: '/pages/forgetpass/forgetpass',
    })
  },
  register(){
    wx.navigateTo({
      url: '/pages/register/register',
    })
  },
  retreat() {
    wx.navigateBack({
      delta: 1
    })
  },
  //发送验证码
  sendMsg(){
    console.log(11111);
    var that = this;
    var currentTime = that.data.currentTime;
    // that.setData({
    //   time: (currentTime-1) + '秒'
    // })
    that.setData({
      disabled: true
    })
    let interval = setInterval(function () {
      that.setData({
        time: (currentTime - 1) + '秒',
       
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取',
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000)
    
  },
  voteTitle(e){
    
    this.data.username = e.detail.value;
   
  },
  voteTitle1(e){
    this.data.password = e.detail.value;
  },
  //登录请求
  loginAjax(){
    let baseURL = "http://192.168.0.106:8090/xzhotel/";
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseURL + 'login',
        method: 'post',
        data: {
          username: this.data.username,
          password: this.data.password
        },
        success: resolve,
        fail: reject,
      })
    })
  },
  longinBtn(){
    let username = this.data.username;
    let password = this.data.password;
    
    console.log(username);
    if (username ==null) {
     
      wx.showToast({
        title: '用户名不能为空',
        icon: 'none',
        duration: 2000,
      })
      
      return;
    }
    if (password ==null) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
        duration: 2000,
      })
      return;
    }

    this.loginAjax().then(res => {
      if(res.data.code===1){
        let _token= res.data.data.__token;
        let userInfo = res.data.data.userInfo;
        let code=res.data.code;
        wx.setStorageSync('_token', _token);
        wx.setStorageSync('userInfo', userInfo);
        wx.setStorageSync('code', code);
        wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000,
            mask:true,
            success:function(){
              setTimeout(function(){
                wx.switchTab({
                  url: '/pages/index/index',
                })
              },2000)
            }
          })
      }
      else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
      // wx.navigateBack({
      //   delta: 1
      // })
    }).catch(err => {

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.setNavigationBarTitle({
        title: '登录',
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})